package com.itheima.mp.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * ClassName: PageQuery
 * Description:
 *
 * @Author HL
 * @Create 2024/7/9 14:26
 * @Version 1.0
 */
@Data
@Schema(description = "分页查询实体")
public class PageQuery {
    @Schema(description = "页码")
    private Integer pageNo = 1;
    @Schema(description = "页码")
    private Integer pageSize = 5;
    @Schema(description = "排序字段")
    private String sortBy;
    @Schema(description = "是否升序")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPageDefaultSortByCreateTime(){
        return toMpPage(OrderItem.desc("create_time"));
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTime(){
        return toMpPage(OrderItem.desc("update_time"));
    }

    public <T> Page<T> toMpPage(String defaultSortBy, Boolean defaultAsc){
        return toMpPage(defaultAsc ? OrderItem.asc(defaultSortBy) : OrderItem.desc(defaultSortBy));
    }
    
    public <T> Page<T> toMpPage(OrderItem ... items){
        //1. 分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        //2. 排序条件
        if(StrUtil.isNotBlank(sortBy)){
            //不为空
            page.addOrder(isAsc ? OrderItem.asc(sortBy) : OrderItem.desc(sortBy));
        }
        else if(items != null){
            //为空 默认排序
            page.addOrder(items);
        }
        return page;
    }
}
